package com.airS.task.fileMgr.controller.gk;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.common.response.BaseResponse;
import com.airS.task.fileMgr.common.response.ResponseCode;
import com.airS.task.fileMgr.model.gk.Employee;
import com.airS.task.fileMgr.model.gk.EmployeeRole;
import com.airS.task.fileMgr.query.gk.EmployeeRoleQuery;
import com.airS.task.fileMgr.security.shiro.realm.SystemAuthorizingRealm;
import com.airS.task.fileMgr.service.gk.EmployeeRoleService;
import com.airS.task.fileMgr.service.gk.EmployeeService;
import com.airS.task.fileMgr.service.gk.RoleService;
import com.airS.task.fileMgr.utils.shiro.SecurityUtils;
import com.airS.task.fileMgr.utils.url.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 人员角色映射管理
 * Created by alan on 2017/3/20.
 */
@Controller
@RequestMapping("/empRole")
public class EmployeeRoleController {

    @Autowired
    private EmployeeRoleService employeeRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private EmployeeService employeeService;

    /**
     * 人员角色映射列表
     * @param employeeRoleQuery
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/list")
    public String list(EmployeeRoleQuery employeeRoleQuery, HttpServletRequest request, ModelMap modelMap){
        employeeRoleQuery.setRequestUrl(UrlUtils.getLookupPathForRequest(request));
        Page<EmployeeRole> employeeRolePage =  employeeRoleService.queryEmployeeRoles(employeeRoleQuery);
        modelMap.put("employeeRolePage", employeeRolePage);
        modelMap.put("allRoles", roleService.getAllRoleToList());
        modelMap.put("allEmployees", employeeService.getAllEmployeeToList());
        return "admin/modules/gk/empRole_list";
    }

    /**
     * 人员角色映射删除
     * @param employeeRoleIds
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    public Object remove(@RequestParam("employeeRoleIds[]") List<Integer> employeeRoleIds){
        BaseResponse baseResponse = new BaseResponse();
        SystemAuthorizingRealm.Principal principal = SecurityUtils.getPrincipal();
        // 数据有效性检查
        if(employeeRoleIds == null || employeeRoleIds.isEmpty()){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            return baseResponse;
        }
        // 人员角色映射删除
        Integer count = employeeRoleService.delEmployeeRoleByIds(employeeRoleIds);
        if(count > 0){
            baseResponse.setMsg("删除成功");
        }else{
            baseResponse.setMsg("删除失败，请重试");
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
        }
        return baseResponse;
    }

    /**
     * 人员角色映射新增页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(ModelMap modelMap){
        modelMap.put("employeeRole", new EmployeeRole());
        modelMap.put("allRoles", roleService.getAllRoleToList());
        modelMap.put("allEmployees", employeeService.getAllEmployeeToList());
        return "admin/modules/gk/empRole_add";
    }

    /**
     * 人员角色映射新增
     * @param employeeRole
     * @return
     */
    @RequestMapping("/add")
    public String add(EmployeeRole employeeRole, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        SystemAuthorizingRealm.Principal principal = SecurityUtils.getPrincipal();
        session.removeAttribute("empRoleBaseResponse");
        // 人员角色映射新增
        employeeRole.setUpdatePeople(null);
        employeeRole.setUpdateTime(null);
        employeeRole.setCreateTime(new Date());
        employeeRole.setCreatePeople(principal.getEmployeeName());
        Integer employeeRoleId = employeeRoleService.addEmployeeRole(employeeRole);
        // 封装业务操作状态
        if(employeeRoleId > 0){
            baseResponse.setMsg("操作成功");
        }else{
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
            baseResponse.setMsg("操作失败，请重试");
        }
        session.setAttribute("empRoleBaseResponse", baseResponse);
        return "redirect:/empRole/list?pclass=p_6&currentClass=c_10";
    }

    /**
     * 人员角色映射修改页面
     * @param modelMap
     * @param query
     * @param session
     * @return
     */
    @RequestMapping("/toModify")
    public String toModify(ModelMap modelMap, EmployeeRoleQuery query, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        session.removeAttribute("empRoleBaseResponse");
        // 数据有效性检查
        if(query.getEmployeeRoleId() == null){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            session.setAttribute("empRoleBaseResponse", baseResponse);
            return "redirect:/empRole/list?pclass=p_6&currentClass=c_10";
        }
        // 封装页面模型
        EmployeeRole employeeRole = employeeRoleService.queryEmployeeRoleById(query.getEmployeeRoleId());
        if(employeeRole.getEmployeeRoleId() == null || employeeRole.getEmployeeRoleId() <= 0){
            baseResponse.setCode(ResponseCode.NOT_EXIST_FAIL);
            baseResponse.setMsg("查找不到当前人员角色映射，请重试");
            session.setAttribute("empRoleBaseResponse", baseResponse);
            return "redirect:/empRole/list?pclass=p_6&currentClass=c_10";
        }
        modelMap.put("employeeRole", employeeRole);
        modelMap.put("allRoles", roleService.getAllRoleToList());
        modelMap.put("allEmployees", employeeService.getAllEmployeeToList());
        return "admin/modules/gk/empRole_modify";
    }

    /**
     * 人员角色映射修改
     * @param employeeRole
     * @param modelMap
     * @param session
     * @return
     */
    @RequestMapping("/modify")
    public String modify(EmployeeRole employeeRole, ModelMap modelMap, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        session.removeAttribute("empRoleBaseResponse");
        // 数据有效性检查
        if(employeeRole.getEmployeeRoleId() == null){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            session.setAttribute("empRoleBaseResponse", baseResponse);
            return "redirect:/empRole/list?pclass=p_6&currentClass=c_10";
        }
        // 人员角色映射修改
        Integer count = employeeRoleService.updateEmployeeRoleById(employeeRole);
        // 封装业务操作状态
        if(count > 0){
            baseResponse.setMsg("操作成功");
        }else{
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
            baseResponse.setMsg("操作失败，请重试");
        }
        session.setAttribute("empRoleBaseResponse", baseResponse);
        return "redirect:/empRole/list?pclass=p_6&currentClass=c_10";
    }

    /**
     * 获取操作结果
     * @param session
     * @return
     */
    @RequestMapping("/getOperaStatus")
    @ResponseBody
    public Object getOperaStatus( HttpSession session){
        Object status = session.getAttribute("empRoleBaseResponse");
        session.removeAttribute("empRoleBaseResponse");
        return status;
    }

}
